package com.senditcertified.mail;

import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;

import com.Utility.MessageManager;
import com.Utility.Utility;
import com.Utility.WebServiceCommunicator.WebServiceFlag;
import com.activities.BaseActivity;
import com.controls.ws.SendMailWSControl;
import com.events.WebServiceCommunicatorListener;
import com.fragments.sendmail.AttachmentsFragment;
import com.fragments.sendmail.AudioFragment;
import com.fragments.sendmail.MessageFragment;
import com.fragments.sendmail.RecepientsFragment;
import com.fragments.sendmail.VideoFragment;
import com.objects.Recepient;

public class SendMail extends BaseActivity implements View.OnClickListener,
		WebServiceCommunicatorListener {
	// private ViewPager mPager;
	private ArrayList<TabInfo> mTabs;
	private TabInfo mCurrentTab = null;
	private View mTabMore;
	private SendMailWSControl mWSSendMail;
	private ProgressDialog mDialog;
	private List<Recepient> mRecipients;
	private String message;
	private String subject;
	private String attachments, audioMessage, videoMessage;
	private View layoutThanks;

	@Override
	protected int initLayout() {
		// TODO Auto-generated method stub
		return R.layout.activity_send_mail;
	}

	@Override
	protected void initViews() {
		mTabMore = findViewById(R.id.tab_more);
		layoutThanks = findViewById(R.id.layout_thanks);
		findViewById(R.id.btn_send).setOnClickListener(this);
		findViewById(R.id.btn_sendanother).setOnClickListener(this);
	}

	@Override
	protected void initComponents() {
		mWSSendMail = new SendMailWSControl(this, this);

		// Add Tab
		mTabs = new ArrayList<SendMail.TabInfo>();
		TabInfo tab = new TabInfo();
		tab.button = findViewById(R.id.tab_message);
		tab.button.setOnClickListener(this);
		tab.fclass = MessageFragment.class;
		mTabs.add(tab);

		tab = new TabInfo();
		tab.button = findViewById(R.id.tab_recepients);
		tab.button.setOnClickListener(this);
		tab.fclass = RecepientsFragment.class;
		mTabs.add(tab);

		tab = new TabInfo();
		tab.button = findViewById(R.id.tab_attachments);
		tab.button.setOnClickListener(this);
		tab.fclass = AttachmentsFragment.class;
		mTabs.add(tab);

		tab = new TabInfo();
		tab.button = findViewById(R.id.tab_audio);
		tab.button.setOnClickListener(this);
		tab.fclass = AudioFragment.class;
		mTabs.add(tab);

		tab = new TabInfo();
		tab.button = findViewById(R.id.tab_video);
		tab.button.setOnClickListener(this);
		tab.fclass = VideoFragment.class;
		mTabs.add(tab);

		doTabChanged(0);
	}

	@Override
	protected void addListener() {
		mTabMore.setOnClickListener(this);
	}

	private class TabInfo {
		Class<?> fclass;
		Fragment fragment;
		View button;

	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.tab_message) {
			doTabChanged(0);
		} else if (id == R.id.tab_recepients) {
			doTabChanged(1);
		} else if (id == R.id.tab_attachments) {
			doTabChanged(2);
		} else if (id == R.id.tab_audio) {
			doTabChanged(3);
		} else if (id == R.id.tab_video) {
			doTabChanged(4);
		} else if (id == R.id.tab_more) {
			if (mTabs.get(0).button.getVisibility() == View.VISIBLE) {
				for (int i = 0; i < 3; i++) {
					mTabs.get(i).button.setVisibility(View.GONE);
				}
				for (int i = 3; i < mTabs.size(); i++) {
					mTabs.get(i).button.setVisibility(View.VISIBLE);
				}
			} else {
				for (int i = 0; i < 3; i++) {
					mTabs.get(i).button.setVisibility(View.VISIBLE);
				}
				for (int i = 3; i < mTabs.size(); i++) {
					mTabs.get(i).button.setVisibility(View.GONE);
				}
			}
		} else if (id == R.id.btn_send) {
			sendMail();
		} else if (id == R.id.btn_sendanother) {
			layoutThanks.setVisibility(View.GONE);
			
			// Re-initial view
			initComponents();
			addListener();
			
			// Show first group
			for (int i = 0; i < 3; i++) {
				mTabs.get(i).button.setVisibility(View.VISIBLE);
			}
			for (int i = 3; i < mTabs.size(); i++) {
				mTabs.get(i).button.setVisibility(View.GONE);
			}
		} else {
		}
	}

	private void doTabChanged(int index) {
		FragmentTransaction ft;
		TabInfo newTab = mTabs.get(index);
		if (mCurrentTab != newTab) {
			ft = getSupportFragmentManager().beginTransaction();
			if (mCurrentTab != null) {
				if (mCurrentTab.fragment != null) {
					ft.detach(mCurrentTab.fragment);
					mCurrentTab.button.setSelected(false);
				}
			}
			if (newTab != null) {
				if (newTab.fragment == null) {
					newTab.fragment = Fragment.instantiate(SendMail.this,
							newTab.fclass.getName());
					ft.add(R.id.pager, newTab.fragment);
				} else {
					ft.attach(newTab.fragment);
				}
			}

			mCurrentTab = newTab;
			mCurrentTab.button.setSelected(true);
			ft.commit();
		}

	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		mCurrentTab.fragment.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void onConnectionOpen(WebServiceFlag flag) {
		if (mDialog == null) {
			mDialog = new ProgressDialog(getParent());
			mDialog.setMessage("Sending...");
			mDialog.setCancelable(false);
		}

		if (flag == WebServiceFlag.SEND_MAIL) {
			if (!mDialog.isShowing())
				mDialog.show();
			mDialog.setMessage("Sending package...");
		}
	}

	@Override
	public void onConnectionError(WebServiceFlag flag, String error) {
		if (mDialog.isShowing()) {
			mDialog.dismiss();
		}
		MessageManager.showToast(getParent(), error);
	}

	@Override
	public void onConnectionDone(WebServiceFlag flag, String response) {

		if (flag == WebServiceFlag.SEND_MAIL) {
			MessageManager.showToast(getParent(), "Package was sent");
			if (mDialog.isShowing()) {
				mDialog.dismiss();
			}
			layoutThanks.setVisibility(View.VISIBLE);
		}
		Log.d("RESPONE", response);
		
	}

	public void sendMail() {
		TabInfo messageTab = mTabs.get(0);
		if (messageTab.fragment == null) {
			MessageManager.showToast(getParent(), "Subject required!");
			return;
		}
		MessageFragment messageFragment = (MessageFragment) messageTab.fragment;
		message = messageFragment.getMessage();
		subject = messageFragment.getSubject();
		if (subject.length() == 0) {
			MessageManager.showToast(getParent(), "Subject required!");
			return;
		}
		if (message.length() == 0) {
			MessageManager.showToast(getParent(), "Message required!");
			return;
		}

		TabInfo recipientTab = mTabs.get(1);
		if (recipientTab.fragment == null) {
			MessageManager.showToast(getParent(), "Recipient required!");
			return;
		}
		RecepientsFragment recepientsFragment = (RecepientsFragment) recipientTab.fragment;
		mRecipients = recepientsFragment.getRecipients();
		if (mRecipients.size() == 0) {
			MessageManager.showToast(getParent(), "Recipient required!");
			return;
		}
		AttachmentsFragment attachmentsFragment = (AttachmentsFragment) mTabs
				.get(2).fragment;
		attachments = "";
		if (attachmentsFragment != null) {
			attachments = attachmentsFragment.getFiles();
		}
		
		AudioFragment auFrag = (AudioFragment) mTabs
				.get(3).fragment;
		audioMessage = "";
		if (auFrag != null) {
			audioMessage = auFrag.getFiles();
		}
		
		VideoFragment viFrag = (VideoFragment) mTabs
				.get(4).fragment;
		videoMessage = "";
		if (viFrag != null) {
			videoMessage = viFrag.getFiles();
		}

		mWSSendMail.sendMail(Utility.getStringPreference(getParent(),
				Utility.KEY_LOGIN_TOKEN, ""), mRecipients, subject, attachments,
				message, audioMessage, videoMessage);
	}
	
}
